package com.kai.controller;

import com.kai.utils.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("file")
public class FileController {
    @RequestMapping("toupload")
    public String toFileUpLoadPage() {
        return "uploadFile";
    }

    @RequestMapping("/upload")
    public String uploadFile(MultipartFile mf) throws IOException {
        //        1.文件上传父目录
        String parentPath = FileUtil.PATH;
//        2.得到当前日期作为文件夹名称
        String dirName = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//        3.构造文件夹对象l
        File dirFile = new File(parentPath, dirName);
        if (!dirFile.exists()) {
//            创建文件夹
            dirFile.mkdir();
        }
//        4.得到文件名
        String oldName = mf.getOriginalFilename();
        String format = UUID.randomUUID().toString();
        String newName = format + oldName;
        File dest = new File(dirFile, newName);
        mf.transferTo(dest);
        return "success";
    }




}
